package com.consul.payment.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {


    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/payment/consul")
    public String paymentConsul() {
        return "springcloud with consul:" + serverPort + "\t"
                + UUID.randomUUID().toString();
    }

    @GetMapping(value="/payment/get/{id}")
    public Object getPaymentById(@PathVariable("id") Long id) {
        System.out.println("payment模块收到id，正在查询：" + id);
        return id + "端口" + serverPort;
    }

    @GetMapping(value="/payment/lb/{id}")
    public Object getPaymentLB(@PathVariable("id") Long id) {
        System.out.println("payment模块收到id，正在查询：" + id);
        return id;
    }

    /**
     * 测试order通过feign调用payment超时
     * @return
     */
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            return serverPort;
        }
    }
    @GetMapping(value="/payment/zipkin")
    public String paymentZipkin() {
        return "hello,i am paymentZipkin server fallback,O(∩_∩)O哈哈~";
    }

}
